﻿using System;
using System.Collections.Generic;

namespace Calculator
{
    public class PostfixExpression : MathExpression
    {
        public PostfixExpression(string expressionParam, ISymbolHandler symbolHandlerParam, IOperatorHandler operatorHandlerParam)
            : base(expressionParam, symbolHandlerParam, operatorHandlerParam)
        {
        }

        public int ComputeFromPostfix()
        {
            var numberStack = new Stack<int>();

            foreach (string piece in Expression.Split('|'))
            {
                if (operatorHandler.IsNumber(piece))
                {
                    numberStack.Push(Int32.Parse(piece));
                }
                else if (operatorHandler.IsOperator(piece))
                {
                    symbolHandler.ComputeOperationOnStack(piece.ToCharArray()[0], numberStack);
                }
            }
            return numberStack.Pop();
        }
    }
}